package com.lcf.order.VO;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Test2 {

    public static void main(String[] args) {
//        Ticket ticket = conver(Ticket.class, "D://ticket.xml");
        List<Ticket> tickets = converForList(Ticket.class, "D://ticket.xml");
        System.out.println(tickets.toString());
    }

    /**
     * xml文件转换对象
     *
     * @param clazz 指定类
     * @param xml   转换的xml字符串
     * @param <T>
     * @return
     */
    public static <T> T conver(Class<T> clazz, String xml) {
        Map map = parse(xml);
        return JSONObject.parseObject(JSON.toJSONString(map), clazz);
    }


    /**
     * xml文件转换为集合
     */
    public static List<Ticket> converForList(Class<Ticket> clazz, String xml) {
        List list = parseForList(xml);
        return JSONObject.parseArray(JSON.toJSONString(list), clazz);
    }


    private static Map parse(String xml) {
        if (StringUtils.isBlank(xml)) {
            throw new NullPointerException("xml不能为空！");
        }
        return parse(xml, new HashMap<>());
    }

    private static List parseForList(String xml) {
        if (StringUtils.isBlank(xml)) {
            throw new NullPointerException("xml不能为空！");
        }
        return parse(xml, new ArrayList<Ticket>());
    }


    private static List parse(String xml, List list) {
        SAXReader reader = new SAXReader();
        //报文转成doc对象
        Document doc = null;
        try {
            InputStream is = new FileInputStream(xml);
            doc = reader.read(is);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //获取根元素，准备递归解析这个XML树
        Element root = doc.getRootElement();
        getCodeForList(root, list);
        return list;
    }

    private static Map parse(String xml, Map<String, Object> map) {
        SAXReader reader = new SAXReader();
        //报文转成doc对象
        Document doc = null;
        try {
            InputStream is = new FileInputStream(xml);
            doc = reader.read(is);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //获取根元素，准备递归解析这个XML树
        Element root = doc.getRootElement();
        getCode(root, map);
        return map;
    }

    private static void getCode(Element root, Map<String, Object> map) {
        if (root.elements() != null) {
            //如果当前跟节点有子节点，找到子节点
            List<Element> list = root.elements();
            //遍历每个节点
            for (Element e : list) {
                if (e.elements().size() > 0) {
                    //当前节点不为空的话，递归遍历子节点；
                    getCode(e, map);
                }
                if ("ticket".equals(e.getName())) {

                }
                //如果为叶子节点，那么直接把名字和值放入map
                if (e.elements().size() == 0) {

                    map.put(e.getName(), e.getTextTrim());
                }
            }
        }
    }

    private static void getCodeForList(Element root, List ticketList) {
        if (root.elements() != null) {
            //如果当前跟节点有子节点，找到子节点
            List<Element> list = root.elements();

            //遍历每个节点
            for (Element e : list) {

                if (e.elements().size() > 0) {
                    //当前节点不为空的话，递归遍历子节点；
                    if ("ticket".equals(e.getName())) {
                        Map<String, Object> map = new HashMap<>();
                        getCode(e, map);
                        ticketList.add(map);
                    } else {

                        getCodeForList(e, ticketList);
                    }

                } else {
                    if ("ticket".equals(e.getName())) {
                        Map<String, Object> map = new HashMap<>();
                        getCode(e, map);
                        ticketList.add(map);
                    }
                }

            }
        }
    }
}
